﻿//-----------------------------------------------------------------------
// <copyright file="MidiOptions.cs" company="Open Source">
//     This file is released under the Microsoft Reciprocal License (Ms-RL)
//     http://www.codeplex.com/wiidrumsynth/license
// </copyright>
//-----------------------------------------------------------------------

namespace WiiDrumSynth
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Sanford.Multimedia.Midi;

    /// <summary>
    /// The options form n the WiiDrumSynth Application
    /// </summary>
    internal partial class MidiOptions : Form
    {
        /// <summary>
        /// The output device's Id
        /// </summary>
        private int outDeviceId;

        /// <summary>
        /// The channel to output any MIDI on
        /// </summary>
        private int channelId = 9;

        /// <summary>
        /// The program to use for any MIDI output
        /// </summary>
        private int programId;

        /// <summary>
        /// Initializes a new instance of the <see cref="MidiOptions"/> class.
        /// </summary>
        internal MidiOptions()
        {
            this.InitializeComponent();

            this.PopulateOutputDevices();
            this.PopulateGMInstruments();
        }

        /// <summary>
        /// Gets or sets the out device id.
        /// </summary>
        /// <value>The out device id.</value>
        internal int OutDeviceId
        {
            get
            {
                return this.outDeviceId;
            }

            set
            {
                this.outDeviceId = value;
                this.outputDeviceCombo.SelectedIndex = this.outDeviceId;
            }
        }

        /// <summary>
        /// Gets or sets the channel id.
        /// </summary>
        /// <value>The channel id.</value>
        internal int ChannelId
        {
            get
            {
                return this.channelId;
            }

            set
            {
                this.channelId = value;
                this.midiChannel.Value = value + 1;
            }
        }

        /// <summary>
        /// Gets or sets the program id.
        /// </summary>
        /// <value>The program id.</value>
        internal int ProgramId
        {
            get
            {
                return this.programId;
            }

            set
            {
                this.programId = value;
                this.midiInstrument.Value = value + 1;
                this.gmMidiInstrument.SelectedIndex = value;
            }
        }

        /// <summary>
        /// Populates the output devices.
        /// </summary>
        private void PopulateOutputDevices()
        {
            this.outputDeviceCombo.Items.Clear();
            if (OutputDevice.DeviceCount > 0)
            {
                for (int i = 0; i < OutputDevice.DeviceCount; i++)
                {
                    this.outputDeviceCombo.Items.Add(OutputDevice.GetDeviceCapabilities(i).name);
                }

                this.outputDeviceCombo.SelectedIndex = this.outDeviceId;
            }
        }

        /// <summary>
        /// Populates the GM instruments.
        /// </summary>
        private void PopulateGMInstruments()
        {
            this.gmMidiInstrument.Items.Clear();
            for (int i = 0; i < 127; i++)
            {
                this.gmMidiInstrument.Items.Add((GeneralMidiInstrument)i);
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the comboBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.outDeviceId = this.outputDeviceCombo.SelectedIndex;
        }

        /// <summary>
        /// Handles the ValueChanged event of the midiChannel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MidiChannel_ValueChanged(object sender, EventArgs e)
        {
            this.channelId = (int)this.midiChannel.Value - 1;
        }

        /// <summary>
        /// Handles the ValueChanged event of the midiInstrument control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MidiInstrument_ValueChanged(object sender, EventArgs e)
        {
            this.programId = (int)this.midiInstrument.Value - 1;
            this.gmMidiInstrument.SelectedIndex = this.programId;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the gmMidiInstrument control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GmMidiInstrument_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.programId = this.gmMidiInstrument.SelectedIndex;
            this.midiInstrument.Value = this.programId + 1;
        }
    }
}
